package com.jerry.base.thread;

import lombok.extern.slf4j.Slf4j;

/**
 * User: Jerry
 * Date: 2024/1/31
 * Time: 22:06
 * Description:
 */
@Slf4j
public class ThreadInterrupt {

    public static void main(String[] args) {
        method();
        interrupt();
    }

    public static void method() {
        Thread thread = new Thread(() -> log.info("线程名字：{}", Thread.currentThread().getName()));
        thread.setName("线程名称");
        // 线程优先级最大值：Thread.MAX_PRIORITY，最小值：Thread.MIN_PRIORITY
        thread.setPriority(Thread.MAX_PRIORITY);
        thread.start();
    }

    public static void interrupt() {
        Thread thread = new Thread(() -> {
           while (true) {
               try {
                   log.info("线程{}睡觉中 zzz ", Thread.currentThread().getName());
                   Thread.sleep(10000);
               } catch (InterruptedException e) {
                   log.info("线程{}被打断", Thread.currentThread().getName());
                   log.info("线程{}准备重新睡觉", Thread.currentThread().getName());
               }
           }
        });
        thread.start();

        for (int i = 0; i < 3; i++) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            thread.interrupt();
        }
    }

}
